package net.tycmc.cty.ctyms.timejob.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.tycmc.functions.ExtTrackPoint;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.cty.ctyms.timejob.common.ListUtil;
import net.tycmc.cty.ctyms.timejob.common.MapUtils;

public final class LoadSubSectionUtil {
	private static Logger log = Logger.getLogger(LoadSubSectionUtil.class);
	
	/**
	 * 载重分段(先进行载重值调整)
	 * 先将载重值进行调整，然后调用载重MME计算方法
	 * @param List<Map<String,Object>> loadSrcList 载重定时信息源数据(需包含 油耗:oilCons，载重:load，行驶里程:runKm，
	 * 行驶时间:runTime，信息ID：MsgT_ID,是否是停留点:ifDisPoint，信息时间:msgTime 车速：vclSpeed）
	 * @param List<Map<String,Object>> pstnSpeedList 速度为0的定时信息对应的位置信息（经度：lng;纬度：lat；
	 * 定时信息ID：MsgT_ID；定时信息时间：msgTime）
	 * @param List<Map<String,Object>> pstnSrcList 位置信息——轨迹查询时才用到、列表计算时未null，按位置信息正序排序（需包含经度：Longitude,纬度：Latitude，
	 * 定时信息ID：MsgT_ID，信息时间:msgTime）
	 * @param List<Map<String,Object>> fenceList 该设备所属机构和所有上级机构的围栏，按创建（修改）时间逆序排序(需包含 围栏类型：OrgFen_Type)
	 * @param boolean needAdjustLoadByLoadType 是否需要进行载重值按照载重类型进行调整
	 * @param Map<String,Object> loadSet 载重设置信息
	 *1.载重区间(double型)
	 * 空载:noLoadBgn-noLoadEnd
	 * 装载:zhuangLoadBgn-zhuangLoadEnd
	 * 满载:fullLoadBgn-fullLoadEnd
	 * 特满载:teLoadBgn-teLoadEnd
	 * 无效区间:invalidBgn-invalidEnd
	 *2.载重类型调整方法 
	 * 空载-载重调整公式： *NoLoadRegM+NoLoadRegR
	 * 装载-载重调整公式： *LoadRegM+LoadRegR
	 * 满载-载重调整公式： *FullLoadRegM+FullLoadRegR
	 * 超满载-载重调整公式： *SpeFulLLoadRegM+SpeFullLoadRegR
	 *3.载重值围栏调整（0：不选；1：选中）：FenRegSet 空值按照0处理
	 *4.围栏类型载重调整方法
	 * 装卸货-载重调整公式中的“乘数”:CLRegM
	 * 装卸货-载重调整公式中的“余数”:CLRegR
	 * 高速服务区-载重调整公式中的“乘数”：ESARegM
	 * 高速服务区-载重调整公式中的“余数”：ESARegR
	 * 高速收费站-载重调整公式中的“乘数”：HTGRegM
	 * 高速收费站-载重调整公式中的“余数”：HTGRegR
	 * 其他-载重调整公式中的“乘数”：OtherRegM
	 * 其他-载重调整公式中的“余数”：OtherRegR 
	 *5.载重调整系数(double型)
	 * 乘数:x
	 * 加数:y
	 *6.MME计算方式(int 型)
	 * MMEType
	 * 		0-车速为0
	 * 		1-超长车速为0(同步提供MME_IntervalMin:间隔-分钟)
	 * @更新历史： jiyongtian 20140708 创建 IEMS3.6.2.2 新增方法
	 **/
	public LoadLines adjustLoadThenSubSection(List<Map<String,Object>> loadSrcList,List<Map<String,Object>> pstnStopList,List<Map<String,Object>> pstnSrcList,
			List<Map<String,Object>> fenceList,Map<String,Object> loadSet,boolean needAdjustLoadByLoadType
		){
		//如果载重源数据为null，则直接返回null
		if(null==loadSrcList || loadSrcList.isEmpty()){
			return null;
		}else{
			/*根据所属载重范围进行载重值调整******start***/
			if(needAdjustLoadByLoadType){
				adjustLoadByLoadType(loadSrcList,loadSet,"load");
			}
			/*根据所属载重范围进行载重值调整******end***/
			
			/*载重值围栏调整******start***/
			if(StringUtil.isValid(MapGetter.getString(loadSet,"FenRegSet"))&&
					MapGetter.getString(loadSet,"FenRegSet").equals("true")){
				if(null!=pstnStopList&&!pstnStopList.isEmpty()){
					/*//经纬度纠偏
					try {
						pstnStopList = ExtTrackPoint.getInstance().pointsRevise(pstnStopList, "lng", "lat");
					} catch (Exception e) {
						log.info("封装载重计算方法中位置纠偏出错。");
					}*/
					//经纬度纠偏
				}
				adjustLoadByFenceType(loadSrcList,pstnStopList,fenceList,loadSet,"load","lng","lat",false);
			}else{
				//载重值围栏调整 未选中，不再进行载重值围栏调整
			}
			/*载重值围栏调整******end***/
			return loadSubSection(loadSrcList, loadSet, pstnSrcList);
		}
	}
	
	/**
	 * 载重分段
	 * @param loadSrcList   载重信息源数据(需包含 油耗:oilCons，载重:load，行驶里程:runKm，行驶时间:runTime，
	 * 是否是停留点:ifDisPoint，信息时间:MsgTime:yyyy-MM-dd HH:mm:ss 字段）
	 * @param params	配置相关参数
	 * 1.载重区间(double型)
	 * 空载:noLoadBgn-noLoadEnd
	 * 装载:zhuangLoadBgn-zhuangLoadEnd
	 * 满载:fullLoadBgn-fullLoadEnd
	 * 特满载:teLoadBgn-teLoadEnd
	 * 无效区间:invalidBgn-invalidEnd
	 * 2.载重调整系数(double型)
	 * 乘数:x
	 * 加数:y
	 * 3.MME计算方式(int 型)
	 * MMEType
	 * 		0-车速为0
	 * 		1-超长车速为0(同步提供MME_IntervalMin:间隔-分钟)
	 * @param pstnSrcList 该段时间内设备位置信息（为null的话，LoadLine中不含位置信息--不需绘制轨迹，可为null）
	 * @return LoadLines
	 */
	public final static LoadLines loadSubSection(List<Map<String,Object>> loadSrcList,Map<String,Object> params,
			List<Map<String,Object>> pstnSrcList){
		if(params==null||params.size()<13)
			return new LoadLines(new ArrayList<LoadLine>());
		List<LoadLine> retValue = new ArrayList<LoadLine>();
		//1.判断 MME分段方式 并分段
		int MMEType = MapGetter.getInt(params, "MMEType");
		if(MMEType == 1){//超长车速为0
			//1.1 MME 时间间隔
			int MME_IntervalMin = MapGetter.getInt(params, "MME_IntervalMin");
			if(MME_IntervalMin > 0)
				//如果 时间间隔>0，采用 超长车速为0 MME分段逻辑
				retValue = loadSubSection_1(loadSrcList,MME_IntervalMin,pstnSrcList);
			else
				//如果 时间间隔==0，采用 车速为0 MME分段逻辑
				retValue = loadSubSection_2(loadSrcList,pstnSrcList);
		}else{//车速为0
			retValue = loadSubSection_2(loadSrcList,pstnSrcList);
		}
		//2.把每段数据 封装成一个 载重段  对象，并添加到List中
		//载重系数
		Map<String,Double> loadFactor = new HashMap<String, Double>();
		loadFactor.put("x", MapGetter.getDouble(params, "x"));
		loadFactor.put("y", MapGetter.getDouble(params, "y"));
		//载重区间
		Map<String,Double> loadInterval = getLoadRange(params);
		for (LoadLine ll : retValue) {
			ll.setLoadFactor(loadFactor);//载重调整 系数赋值
			ll.setLoadInterval(loadInterval);//载重 区间赋值
		}
		//3.创建 载重段集合 对象
		LoadLines retLoadLines =  new LoadLines(retValue);
		return retLoadLines;
	}
	/**
	 * 载重区间赋值
	 * @param params
	 * @return
	 */
	private static Map<String,Double> getLoadRange(Map<String,Object> params){
		Map<String,Double> retValue = new HashMap<String, Double>();
		retValue.put("noLoadBgn", MapGetter.getDouble(params, "noLoadBgn"));
		retValue.put("noLoadEnd", MapGetter.getDouble(params, "noLoadEnd"));
		retValue.put("zhuangLoadBgn", MapGetter.getDouble(params, "zhuangLoadBgn"));
		retValue.put("zhuangLoadEnd", MapGetter.getDouble(params, "zhuangLoadEnd"));
		retValue.put("fullLoadBgn", MapGetter.getDouble(params, "fullLoadBgn"));
		retValue.put("fullLoadEnd", MapGetter.getDouble(params, "fullLoadEnd"));
		retValue.put("teLoadBgn", MapGetter.getDouble(params, "teLoadBgn"));
		retValue.put("teLoadEnd", MapGetter.getDouble(params, "teLoadEnd"));
		retValue.put("invalidBgn", MapGetter.getDouble(params, "invalidBgn"));
		retValue.put("invalidEnd", MapGetter.getDouble(params, "invalidEnd"));
		return retValue;
	}
	/**
	 * 超长车速为0 ，MME分段
	 * @param srcList			源数据
	 * @param MME_Min	时间间隔
	 * @return	
	 */
	private static List<LoadLine> loadSubSection_1(List<Map<String,Object>> srcList,int MME_Min,
			List<Map<String,Object>> pstnSrcList){
		List<LoadLine> loadLines = new ArrayList<LoadLine>();
		if(srcList==null||srcList.size()==0)
			return loadLines;
		//1.按车速为0 分段
		loadLines = loadSubSection_2(srcList,pstnSrcList);
		//2.判断停留段 的信息时间差
		List<LoadLine> newRetValue = new ArrayList<LoadLine>();//返回结果
		LoadLine currLine = new LoadLine();
		for(int i=0;i<loadLines.size();i++){
			LoadLine currll = loadLines.get(i);//取出当前段
			if(currll.isRunning()){
				currLine.add(currll);//运行段，直接 添加
			}else{//停留段
				if(currll.getMsgTimeCha()<MME_Min){//小于间隔
					currLine.add(currll);
				}else{//大于间隔
					/*如果当前段是第一段并且停留段、大于间隔，则需要赋值处理
					 * 不是第一段的当前停留段大于间隔时 currLine不可能为新建对象
					 * 因此下面的两种逻辑任一都可，jiyongtian20130807,采用第一种
					 **/
					/*第一种**/
					if(i==0){
						currLine.add(currll);
						newRetValue.add(currLine);
					}else{
						/*
						 * 此时currLine是当前段的前一段，loadLines.get(i)当前段，
						 * 所以两者都添加到返回结果
						 * 当前段的下一段一定是运行段——载重分段的业务意义决定，故新建当前段
						 */
						newRetValue.add(currLine);
						newRetValue.add(loadLines.get(i));
					}
					/*第二种
					if(currLine.getLoadSrcList().size()==0){
						currLine.add(currll);
					}
					*/
					currLine = new LoadLine();
				}
			}
		}
		if(currLine.getLoadSrcList().size()>0)
			newRetValue.add(currLine);
		return newRetValue;
	}
	/**
	 * 车速为0 ，MME分段
	 * @param srcList 源数据
	 * @return
	 * 更新：jiyongtian 2013-10-15 分段时位置信息仅重复一个点
	 */
	private static List<LoadLine> loadSubSection_2(List<Map<String,Object>> srcList,List<Map<String,Object>> pstnSrcList){
		List<LoadLine> retValue = new ArrayList<LoadLine>();
		if(srcList==null||srcList.size()==0)
			return retValue;
		boolean hasPstnPoint = false;
		if(pstnSrcList!=null&&pstnSrcList.size()>0){
			hasPstnPoint = true;
		}
		List<Map<String,Object>> currLine = new ArrayList<Map<String,Object>>();
		Map<String,List<Map<String,Object>>> pstnMap = null;
		List<Map<String,Object>> currPstnLine = null;
		if(hasPstnPoint){
			currPstnLine = new ArrayList<Map<String,Object>>(); 
			pstnMap = groupBy("MsgT_ID","msgTime", pstnSrcList);
		}
		String key = null;//定时信息ID，关联位置点使用
		List<Map<String,Object>> currPstnList = null;
		for (int i=0;i<srcList.size()-1;i++) {
			//1.第一个点 放入第一段
			Map<String,Object> currPoint = srcList.get(i);
			/*
			if(hasPstnPoint){
				key = MapGetter.getString(currPoint, "MsgT_ID");
				currPstnList = pstnMap.remove(key);
				if(currPstnList!=null)
				currPstnLine.addAll(currPstnList);
			}
			*/
			if(hasPstnPoint){
				key = MapGetter.getString(currPoint, "MsgT_ID")+"_"+MapGetter.getString(currPoint, "msgTime");
				currPstnList = pstnMap.remove(key);
			}
			//1.第一个点 放入第一段
			if(i==0){
				currLine.add(currPoint);
				if(hasPstnPoint){
					if(null!=currPstnList&&currPstnList.size()>0){
						currPstnLine.addAll(currPstnList);
					}
				}
			}else{
				//2.判断当前点是不是分段点
				if(isCutPoint(srcList.get(i-1),currPoint,srcList.get(i+1))){//是分段点
					currLine.add(currPoint);//当前段 添加结束点
					LoadLine ll = new LoadLine();//创建对象
					ll.setLoadSrcList(currLine);
					if(hasPstnPoint){
						if(null!=currPstnList&&currPstnList.size()>0){
							currPstnLine.addAll(currPstnList);
						}
						ll.setPstnSrcList(currPstnLine);
						//创建新段位置信息，同时将上一段位置信息的最后一个点进行复制保存
						if(null!=currPstnLine&&currPstnLine.size()>0){
							Map<String,Object> lastPoint = currPstnLine.get(currPstnLine.size()-1);
							currPstnLine =  new ArrayList<Map<String,Object>>();
							currPstnLine.add(lastPoint);
						}else{
							currPstnLine =  new ArrayList<Map<String,Object>>();
						}
					}
					retValue.add(ll);
					currLine =  new ArrayList<Map<String,Object>>();//创建新的段
					currLine.add(currPoint);//新段 添加 开始点
				}else{// 不是分段点，放入 当前段
					currLine.add(currPoint);
					if(hasPstnPoint){
						if(null!=currPstnList&&currPstnList.size()>0){
							currPstnLine.addAll(currPstnList);
						}
					}
				}
			}
		}
		currLine.add(srcList.get(srcList.size()-1));//添加 结束点
		LoadLine ll = new LoadLine();//最后一段
		ll.setLoadSrcList(currLine);
		if(hasPstnPoint){
			currPstnList = pstnMap.remove(MapGetter.getString(srcList.get(srcList.size()-1), "MsgT_ID"));
			if(null!=currPstnList&&currPstnList.size()>0){
				currPstnLine.addAll(currPstnList);
			}
			ll.setPstnSrcList(currPstnLine);
		}
		retValue.add(ll);
		currLine = null;
		currPstnLine = null;
		pstnMap = null;
		return retValue;
	}
	/**
	 * 判断currPoint 是否为分段点（车速为0 MME分段判断）
	 * @param prePoint
	 * @param currPoint
	 * @param nextPoint
	 * @return
	 */
	private static boolean isCutPoint(Map<String,Object> prePoint,Map<String,Object> currPoint,Map<String,Object> nextPoint){
		 /*如果prePoint为null，表示currPoint为开始点，开始点 视为 分段点
		  * 如果nextPoint为null，表示currPoint为结束点，结束点 视为 分段点*/
		if(prePoint==null||nextPoint==null)
			return true;
		//如果当前点 为1（停留点），上一点或下一点 有一个为0，则表示currPoint 为分段点
		if(MapGetter.getString(currPoint, "ifDisPoint").equals("1")&&((MapGetter.getString(prePoint, "ifDisPoint").equals("0"))||
				MapGetter.getString(nextPoint, "ifDisPoint").equals("0"))){
			return true;
		}
		return false;
	}
	/**
	 * 分组
	 * @param key1 	分组字段名称一般是表主键
	 * @param key2      分组字段名称2 为null或""时仅使用key1，否则key1+"_"+key2
	 * @param srcList	源数据
	 * @return	Map<key,对应的List>
	 */
	public static final Map<String,List<Map<String,Object>>> groupBy(String key1,String key2,List<Map<String,Object>> srcList){
		Map<String,List<Map<String,Object>>> retMap = new HashMap<String, List<Map<String,Object>>>();
		StringBuffer mapKey = new StringBuffer();
		List<Map<String,Object>> mapList = null;
		for (Map<String, Object> map : srcList) {
			mapKey.append(MapGetter.getString(map, key1));
			
			if(StringUtil.isValid(key2)){
				mapKey.append("_"+MapGetter.getString(map, key2));
			}
			
			mapList = retMap.get(mapKey);
			if(mapList==null){
				mapList = new ArrayList<Map<String,Object>>();
				mapList.add(map);
				retMap.put(mapKey.toString(), mapList);
			}else{
				mapList.add(map);
			}
		}
		return retMap;
	}
	/**
	 * 将载重值根据载重类型进行调整
	 * @更新历史： jiyongtian 20140708 创建 IEMS3.6.2.2 新增方法
	 **/
	private void adjustLoadByLoadType(List<Map<String, Object>> loadSrcList,Map<String,Object> loadSet,String loadKey) {
		if(!StringUtil.isValid(loadKey)){
			loadKey = "load";
		}
		double noLoadBgn = MapGetter.getDouble(loadSet, "noLoadBgn");
		double noLoadEnd = MapGetter.getDouble(loadSet, "noLoadEnd");
		double zhuangLoadBgn = MapGetter.getDouble(loadSet, "zhuangLoadBgn");
		double zhuangLoadEnd = MapGetter.getDouble(loadSet, "zhuangLoadEnd");
		double fullLoadBgn = MapGetter.getDouble(loadSet, "fullLoadBgn");
		double fullLoadEnd = MapGetter.getDouble(loadSet, "fullLoadEnd");
		double teLoadBgn = MapGetter.getDouble(loadSet, "teLoadBgn");
		double teLoadEnd = MapGetter.getDouble(loadSet, "teLoadEnd");
		double NoLoadRegM = MapGetter.getDouble(loadSet, "NoLoadRegM"); 
		double NoLoadRegR = MapGetter.getDouble(loadSet, "NoLoadRegR");
		double LoadRegM = MapGetter.getDouble(loadSet, "LoadRegM");
		double LoadRegR = MapGetter.getDouble(loadSet, "LoadRegR");
		double FullLoadRegM = MapGetter.getDouble(loadSet, "FullLoadRegM"); 
		double FullLoadRegR = MapGetter.getDouble(loadSet, "FullLoadRegR");
		double SpeFullLoadRegM = MapGetter.getDouble(loadSet, "SpeFullLoadRegM");
		double SpeFullLoadRegR = MapGetter.getDouble(loadSet, "SpeFullLoadRegR");
		for(Map<String,Object> oneSrc:loadSrcList){
			double load = StringUtil.isValid(MapGetter.getString(oneSrc, loadKey))?Double.parseDouble(MapGetter.getString(oneSrc, loadKey)):0;
			if(load>=noLoadBgn&&load<=noLoadEnd){//空载
				oneSrc.put(loadKey, (load*NoLoadRegM+NoLoadRegR));
			}else if(load>=zhuangLoadBgn&&load<=zhuangLoadEnd){//装载
				oneSrc.put(loadKey, (load*LoadRegM+LoadRegR));
			}else if(load>=fullLoadBgn&&load<=fullLoadEnd){//满载
				oneSrc.put(loadKey, (load*FullLoadRegM+FullLoadRegR));
			}else if(load>=teLoadBgn&&load<=teLoadEnd){//超满载
				oneSrc.put(loadKey, (load*SpeFullLoadRegM+SpeFullLoadRegR));
			}else{//没在任意载重类型内，载重值不调整
				
			}
		}
	}
	/**
	 * 将载重值根据所在的围栏类型进行调整
	 * @更新历史：jiyongtian 20140708创建IEMS3.6.2.2
	 **/
	private void adjustLoadByFenceType(List<Map<String, Object>> loadSrcList,List<Map<String,Object>> pstnSpeedList,
			List<Map<String, Object>> fenceList,Map<String, Object> loadSet,String loadKey,String lngKey,String latKey,boolean keyChange) {
		Map<String,List<Map<String,Object>>> pstnMap = null;
		boolean pstnFlag = false;
		if(null!=pstnSpeedList&&!pstnSpeedList.isEmpty()){
			pstnFlag = true;
			pstnMap = new ListUtil().groupByKeys("MsgT_ID","msgTime",pstnSpeedList);
		}
		boolean fenceFlag = false;
		if(null!=fenceList&&!fenceList.isEmpty()){
			fenceFlag = true;
		}
		
		double CLRegM = MapGetter.getDouble(loadSet, "CLRegM");
		double CLRegR = MapGetter.getDouble(loadSet, "CLRegR");
		double ESARegM = MapGetter.getDouble(loadSet, "ESARegM");
		double ESARegR = MapGetter.getDouble(loadSet, "ESARegR");
		double HTGRegM = MapGetter.getDouble(loadSet, "HTGRegM");
		double HTGRegR = MapGetter.getDouble(loadSet, "HTGRegR");
		double OtherRegM = MapGetter.getDouble(loadSet, "OtherRegM");
		double OtherRegR = MapGetter.getDouble(loadSet, "OtherRegR");
		double load = 0;
		int type = 0;
		float vclSpeed = 0;
		for(int i=0;i<loadSrcList.size();i++){
			Map<String,Object> oneLoad = loadSrcList.get(i);
			vclSpeed = StringUtil.isValid(MapGetter.getString(oneLoad,"vclSpeed"))?Float.parseFloat(MapGetter.getString(oneLoad,"vclSpeed")):-1;
			if(i==0||vclSpeed==0){//第一个点和速度为0的点 获取所属的围栏类型
				/*if(!timingContainloglat){
					type = this.getLoadPstnBelongFenceType(pstnMap.remove(MapGetter.getString(oneLoad, "MsgT_ID")+"_"+MapGetter.getString(oneLoad, "msgTime")).get(0),
							fenceList,"Longitude","Latitude",true);
				}else{
					type = this.getLoadPstnBelongFenceType(oneLoad,fenceList,"","",false);
				}*/
				if(!pstnFlag||!fenceFlag){
					type = 4;
				}else{
					if(i==0&&vclSpeed!=0){
						type = 4;
					}else{
						type = this.getLoadPstnBelongFenceType(pstnMap.get(MapGetter.getString(oneLoad,"MsgT_ID")+"_"+MapGetter.getString(oneLoad,"msgTime")).get(0),fenceList,lngKey,latKey,keyChange);
					}
				}
			}else{
				//不是第一个点、速度为0的点， 直接进行载重值调整
			}
			load = StringUtil.isValid(MapGetter.getString(oneLoad, loadKey))?Double.parseDouble(MapGetter.getString(oneLoad, loadKey)):0;
			switch(type){
			case 1:
				oneLoad.put(loadKey,(load*CLRegM+CLRegR));
				break;
			case 2:
				oneLoad.put(loadKey,(load*ESARegM+ESARegR));
				break;
			case 3:
				oneLoad.put(loadKey,(load*HTGRegM+HTGRegR));
				break;
			case 4:
				oneLoad.put(loadKey,(load*OtherRegM+OtherRegR));
				break;
			default:
				break;
			}
		}
	}
	/**
	 * 获得位置点 属于围栏类型
	 * 1：装卸货区、2：高速服务区、3：高速收费区、4：其他
	 **/
	private int getLoadPstnBelongFenceType(Map<String, Object> onePstn,
			List<Map<String, Object>> fenceList,String logKey,String latKey,boolean keyChange) {
		int reInt = 0;
		if(null==fenceList||fenceList.isEmpty()){
			reInt = 4;
		}else{
			//载重位置点
			Map<String,Object> targetPlace = null;
			if(!keyChange){
				targetPlace = onePstn;
			}else{
				targetPlace = new HashMap<String,Object>();
				targetPlace.put("lng", MapGetter.getString(onePstn, logKey));
				targetPlace.put("lat", MapGetter.getString(onePstn, latKey));
			}
			
			Map<String,Object> fence = null;
			int rs = 0;
			for(Map<String,Object> morePlace:fenceList){
				//判断车辆位置与围栏位置的关系 0：车辆位置不存在或无围栏信息；1：点在圆内；2：点在圆外；3：点在圆上
//				rs = MapUtils.PointAndCircleRelationShip(targetPlace,morePlace);
				rs = MapUtils.PointAndSquareRelationShip(targetPlace,morePlace);
				if(rs==1||rs==3){
					fence = morePlace;
					break;
				}else{
					continue;
				}
			}
			if(null==fence||fence.isEmpty()){
				reInt = 4;
			}else{
				reInt = StringUtil.isValid(MapGetter.getString(fence, "OrgFen_Type"))?Integer.parseInt(MapGetter.getString(fence, "OrgFen_Type")):0;
			}
		}
		return reInt;
	}
}